﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SpellChecker
{
    public class SpellCheckerMain 
    {
        private ISpellCheck _context = null;
        private string _lang="en";
        private string _action = "";
        private HttpContext _con = null;
        public SpellCheckerMain(string driver, string lang, string action, HttpContext context)
        {
            _lang = lang;
            _action = action;
            _con = context;
            switch (driver.ToLower())
            {
                case "hunspell":
                    _context = new NHunSpellCheck();
                    break;
                default:
                    throw new Exception("not support this drive：" + driver);
            }
        }
        public string Excute()
        {
            switch (_action.ToLower())
            {
                case "get_incorrect_words":
                    return GetIncorrectWords(_con.Request.Form["text[]"]);
                case "get_suggestions":
                    return GetSuggest(_con.Request.Form["word"]);    
                
            }
            return string.Empty;
        }
        private string GetIncorrectWords(string text)
        {
            var result = _context.GetIncorrectWords(text.Split(' ').ToList(), _lang);
            return Newtonsoft.Json.JsonConvert.SerializeObject(new { outcome = "success", data = result }).Replace("[", "[[").Replace("]", "]]");
        }

        private string GetSuggest(string word)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(_context.GetSuggest(word,_lang));
        }
    }
}
